using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace SimplePong
{
    public class Ball
    {

        Texture2D texture;
        Vector2 position;
        Vector2 velocity;
        Rectangle rectangle;
        bool isStopped;

        SoundEffect boundSound;

        public bool IsStopped
        {
            get { return isStopped; }
            set { isStopped = value; }
        }


        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public float VelocityX
        {
            get { return velocity.X; }
            set { velocity.X = value; }
        }

        public float VelocityY
        {
            get { return velocity.Y; }
            set { velocity.Y = value; }
        }
        

        public Rectangle Rectangle
        {
            get { return rectangle; }
            set { rectangle = value; }
        }


        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public float PositionX
        {
            get { return position.X; }
            set { position.X = value; }
        }

        public float PositionY
        {
            get { return position.Y; }
            set { position.Y = value; }
        }


        public Ball()
        {
            texture = GameGlobals.content.Load<Texture2D>("Textures/ball");
            boundSound = GameGlobals.content.Load<SoundEffect>("Sounds/b");
            //Default velocity and position
            ResetBall();
            
            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);

        }

        public void Update()
        {
            if (!isStopped)
            {
                //move the ball according the velocity
                position += velocity;

                //check if the ball is out the upper/lower bounds
                if (position.Y <= 0 || 
                    position.Y + texture.Height >= GameGlobals.graphicsManager.PreferredBackBufferHeight)
                {
                    velocity.Y *= -1;
                    boundSound.Play();
                }

                //Update ball collision rectangle
                UpdateRectangle();
            }
        }

        public void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(texture, position, Color.White);
        }

        private void UpdateRectangle()
        {
            rectangle.X = (int)position.X;
            rectangle.Y = (int)position.Y;
        }

        public void ResetBall()
        {
            //Center ball sprite at screen
            position = new Vector2(GameGlobals.graphicsManager.PreferredBackBufferWidth / 2 - texture.Width / 2,
                                   GameGlobals.graphicsManager.PreferredBackBufferHeight / 2 - texture.Height / 2);

            velocity = new Vector2(5,5);
            UpdateRectangle();
        }


        public void IncreaseSpeed()
        {
            Vector2 speedUp = new Vector2(1.05f, 1.05f);
            velocity = velocity * speedUp;
        }


    }
}
